﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ArchiveSystem.ShowBusy
{
   public class MyThread
    {
        private Thread th = null;
        private bool _IsStopped;
        private bool _Suspended;
        private bool _Started;

       private Busy by=null;
       public Busy busy
       {
           set { by = value; }
       }

       public delegate void ThreadWorkEventHandler();
       public event ThreadWorkEventHandler ThreadWork;
       
       public Thread thread
       {
           get
           { return th; }
       }

       public bool Stopped
       {
           get
           { return _IsStopped; }
       }
       public bool Suspended
       {
           get
           { return _Suspended; }
       }
       public bool Started
       {
           get
           { return _Started; }
       }


       public void Start()
       {
           if (!_Started)
           {
               th = new Thread(new ThreadStart(ThreadWork));
               th.IsBackground = true;
               _IsStopped = false;
               _Started = true;
               th.Start();
           }

       }
      
        public void Suspend()
        {
            if (_Started && !_Suspended)
            {
                _Suspended = true;
                //mUnique.WaitOne();
                th.Suspend();
            }
        }
      
        public void Resume()
        {
            if (_Started && _Suspended)
            {
                _Suspended = false;
                //mUnique.ReleaseMutex();
                th.Resume();
            }
        }
      
        public void Stop()
        {
            if (by != null)
                by.CloseForm(null);

            ThreadWork = null;

            if (_Started)
            {
                if (_Suspended)
                    Resume();

                _Started = false;
                _IsStopped = true;
                th.Abort();
                //th.Join();
               
            }
        }

        delegate void SetControlSourceCallback(Control cl, object source);
       public void SetControlSource(Control cl, object source)
       {
           if (cl.InvokeRequired)
           {
               SetControlSourceCallback scb = new SetControlSourceCallback(SetControlSource);
               cl.Invoke(scb, new object[] { cl, source });
               return;
           }

           if (cl is DataGridView)
           {
               if (((DataGridView)cl).DataSource != null)
                   ((BindingSource)((DataGridView)cl).DataSource).Dispose();

               if (source == null)
                   ((DataGridView)cl).DataSource = null;
               else
                   ((DataGridView)cl).DataSource = new BindingSource(source, null);
           }


       }


       delegate void DelDataTableRowCallback(DataTable dt, DataRow dr,Control cl);
       public void DelDataTableRow(DataTable dt, DataRow dr, Control cl)
       {
           if (cl.InvokeRequired)
           {
               DelDataTableRowCallback drc = new DelDataTableRowCallback(DelDataTableRow);
               cl.Invoke(drc, new object[] { dt, dr,cl });
               return;
           }
           
           dt.Rows.Remove(dr);
       }


    }
}
